﻿using BlueDot.Mobile.Core;
using SqlMergeReplication.Model;
using SqlMergeReplication.Views;

namespace SqlMergeReplication.Controllers
{
    public class AuthenticationController : ControllerBase, IAuthenticationController
    {
        #region IAuthenticationController Members

        /// <summary>
        /// Authenticates the user.
        /// </summary>
        /// <param name="userName">Name of the user.</param>
        /// <param name="password">The password.</param>
        /// <returns></returns>
        public bool AuthenticateUser(string userName, string password)
        {
            Guard.ArgumentNotNullOrEmptyString(userName, "userName");
            Guard.ArgumentNotNullOrEmptyString(password, "password");

            //TODO:Add specific Authentication logic here

            if ((userName != "asdf" || password != "asdf"))
            {
                StateBag[ApplicationStateBagItems.CURRENT_USER].Value = null;
                return false;
            }

            var user = new User
                           {
                               FirstName = "Test",
                               LastName = "User",
                               UserName = userName,
                               Password = password,
                               UserID = 1
                           };

            StateBag[ApplicationStateBagItems.CURRENT_USER].Value = user;

            return true;
        }

        // Callbacks in controllers are registered by type strings in the action framework.
        // If you change the name of this callback make sure you update the designer files 
        // of any views that reference this callback.
        public WorkflowRedirectCallback LogOutUser()
        {
            StateBag[ApplicationStateBagItems.CURRENT_USER].Value = null;

            return new WorkflowRedirectCallback
                       {
                           WorkflowRedirectAction = WorkflowRedirectAction.RedirectWorkflow,
                           NextWorkflowItem = typeof(LoginView)
                       };
        }

        #endregion
    }
}